<?php
Shop::register('css/shop.css');

if($this->id == 'shoppingCart')
	$this->renderPartial('/order/waypoint', array('point' => 0));

if(!isset($products)) 
	$products = Shop::getCartContent();

if(!isset($this->breadcrumbs) || ($this->breadcrumbs== array()))
	$this->breadcrumbs = array(
			Shop::t('Shop') => array('//shop/products/'),
			Shop::t('Shopping Cart'));
?>




<?php
if($products) {
	echo '<table cellpadding="0" cellspacing="0" class="shopping_cart">';
	printf('<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>',
			Shop::t('Image'),
			Shop::t('Amount'),
			Shop::t('Product'),
            Shop::t('Distributor'),
            Shop::t('Price'),
            Shop::t('Tax'),
			Shop::t('Actions')
);

	foreach($products as $position => $product) {
		if(@$model = Product::model()->findByPk($product['product_id'])) {
			printf('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s - %s</td></tr>',
					$model->getImage(),
					CHtml::textField('amount_'.$position,
						$product['amount'], array(
							'size' => 4,
							'class' => 'amount_'.$position,
							)
						),
					$model->name,
                    Users::model()->findByPk($product['distributor'])->full_name,
                    DistributorProduct::model()->findByAttributes(array('distributor_id' => $product['distributor']))->getPrice($product['amount']).' vnd',
                    $model->tax->percent.' %',
					CHtml::link(Shop::t('Remove'), array(
							'//shop/shoppingCart/delete',
							'id' => $position), array(
								'confirm' => Shop::t('Are you sure?'))),
                    CHtml::link(Shop::t('Buy'), array(
                                '//shop/order/create&cart_id='.$position), array('class'=>'btn-next'))
					);
                        if (isset($product['ShippingMethod']))
            $shippingMethod = TransporterMethod::model()->findByPk($product['ShippingMethod']);
                        else
                            $shippingMethod = null;
            if ($shippingMethod)
            printf('<tr><td></td><td>1</td><td>%s</td><td></td><td class="text-right">%s</td><td class="text-right">%s</td><td>%s</td></tr>',
				Shop::t($shippingMethod->title),
				Shop::priceFormat($shippingMethod->unit_price),
				Shop::priceFormat($shippingMethod->unit_price),
				CHtml::link(Shop::t('edit'), array('//shop/shippingMethod/choose&cart_id='.$position))
				);
			Yii::app()->clientScript->registerScript('amount_'.$position,"
					$('.amount_".$position."').keyup(function() {
						$.ajax({
							url:'".$this->createUrl('//shop/shoppingCart/updateAmount')."',
							data: $('#amount_".$position."'),
							success: function(result) {
							$('.amount_".$position."').css('background-color', 'lightgreen');
							$('.widget_amount_".$position."').css('background-color', 'lightgreen');
							$('.widget_amount_".$position."').html($('.amount_".$position."').val());
							$('.price_".$position."').html(result);	
							$('.price_total').load('".$this->createUrl(
							'//shop/shoppingCart/getPriceTotal')."');
							},
							error: function() {
							$('#amount_".$position."').css('background-color', 'red');
							},

							});
				});
					");
			}
}

echo '</table>';
?>


<div class="clear"></div>
<p class="price_total radius-8px"> <?php echo Shop::getPriceTotal() ?> </p>
<?php if(Yii::app()->controller->id != 'order') : ?>
    
<p class="buttons">
    
    <?php  echo CHtml::link(Shop::t('Buy this products'), array('//shop/order/create'), array('class'=>'btn-next')); ?>
    <?php  echo CHtml::link(Shop::t('Buy additional Products'), array( '//shop/products'), array('class'=>'btn-previous')); ?>
</p>
<?php endif; ?>
<?php } else { ?> 
    
    <p class="no-items radius-8px "><?php echo Shop::t('Your shopping cart is empty'); } ?></p>


